package com.dy.nygl.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dy.nygl.pojo.Room;
import com.dy.nygl.service.RoomService;

@Controller
@RequestMapping("/room")
public class RoomController {

	@Resource(name="roomService") 
	private RoomService roomService;
	
	@RequestMapping("/getRoom.action")
	@ResponseBody    //由于要向页面返回数据，所以必须加注解
	public List<Map<String, Object>> getRoom(HttpServletRequest request,HttpServletResponse response) {	
		
		if(request.getParameter("buildingId")!=null && !request.getParameter("buildingId").equals("")) {
			int buildingId = Integer.parseInt(request.getParameter("buildingId"));
			List<Room> roomList = roomService.getRoom(buildingId);
			
			if(roomList != null) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				for(int i=0; i<roomList.size(); i++) {
					Map<String, Object> map = new LinkedHashMap <String, Object>();
					map.put("id",roomList.get(i).getId());
					map.put("name", roomList.get(i).getName());
					list.add(map);					
				}	
				return list;
			}
		}	
		return null;
	}
	
}
